<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>

        .container{
            position: relative;
            display: flex;width: 500px;height:50px;border: 1px rgb(49, 142, 170) solid;margin: 100px auto;
        }

        .keywords{
            flex:1;
            border: none;
            outline: none;
            text-indent: 10px;
            font-size: 18px;
        }
        
        .btn{   
            outline: none;
            width: 100px;
            cursor: pointer;
            color: #fff;
            border: none;
            background: rgb(49, 142, 170)
        }
        .layer{
            width: calc(100% - 100px);
            background: #fff;
            border: 1px solid rgb(77, 177, 207);
            position: absolute;
            top: 100%;
            left: -1px;
            display: none;
        }
        .layer div{
            padding: 5px
        }

        .layer div:hover{
            cursor: pointer;
            background: #eee;
        }
    </style>
</head>

<body>

    <div class="container">
        <input type="text" class="keywords">
        <button class="btn">搜索</button>
        <div class="layer">
            <script id="temp" type="text/html">
                {{ each $data item  i}}
                    <div>{{item}}</div>
                {{/each}}
            </script>
        </div>
    </div>
    <div style="height:1000px"></div> 
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script src="js/artTemplate/artTemplate.js"></script>
    <script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>

    <script>
        $(() => {
            var keywords = ""
            var oldKeywordsList = []
            if ($.cookie("oldKeywordsList")) {
                oldKeywordsList = JSON.parse($.cookie("oldKeywordsList"))
            }

            $(window).scroll(function () {
                setTimeout(() => {
                    $(".layer").hide()
                }, 150);
            })

            $(".keywords").focus(function () {
                $(".layer").show()
                initKeywordsList()
            }).blur(function () {
                setTimeout(() => {
                    $(".layer").hide()
                }, 150);
            }).on("input", function () {
                initKeywordsList()
            }).keydown(e => {
                if (e.keyCode === 13) search()
            })

            function initKeywordsList() {
                keywords = $(".keywords").val()
                var arr = oldKeywordsList.filter(item => item.startsWith(keywords))
                $(".layer").html(template("temp", arr))
            }

            $(".layer").on("mouseenter", "div", function () {
                $(".keywords").val($(this).text().trim())
            }).on("mouseleave", "div", function () {
                $(".keywords").val(keywords)
            }).on("click", "div", function () {
                keywords = $(this).text().trim()
            })

            $(".btn").click(() => {
                search()
            })

            function search() {
                var keywords = $(".keywords").val().trim()
                if (!oldKeywordsList.includes(keywords))
                    oldKeywordsList.push(keywords)
                $.cookie("oldKeywordsList", JSON.stringify(oldKeywordsList), {
                    expires: 30
                })
            }
        })
    </script>

</body>

</html>